import { Provider } from '@nestjs/common';
import { ApiConfig, ApiConfigKit } from 'tnwx';
import { WECHAT_CLIENT, WECHAT_MODULE_OPTIONS } from './wechat.constants';
import {
  WechatModuleAsyncOptions,
  WechatModuleOptions,
} from './wechat.interface';

export class WechatClientError extends Error {}
export interface WechatClient {
  defaultKey: string;
  // clients: Map<string, Wechat.Redis>;
  // size: number;
}

export const createClient = (): Provider => ({
  provide: WECHAT_CLIENT,
  useFactory: (options: WechatModuleOptions): ApiConfig => {
    const { appId, appSecret, token } = options;
    const apiConfig = new ApiConfig(appId, appSecret, token);
    ApiConfigKit.putApiConfig(apiConfig);
    return apiConfig;
  },
  inject: [WECHAT_MODULE_OPTIONS],
});

export const createAsyncClientOptions = (options: WechatModuleAsyncOptions) => {
  return {
    provide: WECHAT_MODULE_OPTIONS,
    useFactory: options.useFactory,
    inject: options.inject,
  };
};
